1634E - Fair Share - CodeForces Solution


constructive algorithms data structures dfs and similar graph matchings graphs *2400

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int,int> PII;
#define ft first
#define sd second
const int N=4e5+10;
const int mod=1e9+7;
int n,m;
int idx,deg[N];
vector<PII>g[N];
vector<int>a[N];
map<int,int>mp;
void dfs(int u,int x){
	while(deg[u]){
		deg[u]--;
		int v=g[u][deg[u]].ft;
		if(a[min(u,v)][g[u][deg[u]].sd]!=-1)continue;
		a[min(u,v)][g[u][deg[u]].sd]=x;
		dfs(v,x^1);
	}
}
signed main(){
	ios::sync_with_stdio(false);cin.tie(0);
	cin>>n;
	idx=n;
	for(int i=1;i<=n;i++){
		cin>>m;
		a[i].resize(m+1,-1);
		for(int j=1;j<=m;j++){
			int x;cin>>x;
			if(mp[x]==0)mp[x]=++idx;
			x=mp[x];
			g[i].push_back(make_pair(x,j));
			g[x].push_back(make_pair(i,j));
			deg[i]++;
			deg[x]++;
		}
	}
	for(int i=1;i<=idx;i++){
		if(deg[i]%2){
			cout<<"NO\n";
			return 0;
		}
	}
	for(int i=1;i<=idx;i++)dfs(i,0);
	cout<<"YES\n";
	for(int i=1;i<=n;i++){
		for(int j=1;j<a[i].size();j++){
			if(a[i][j]==0)cout<<'L';
			else cout<<'R';
		}
		cout<<'\n';
	}
}


Comments

Submit
0 Comments
More Questions

1476E - Pattern Matching
1107A - Digits Sequence Dividing
1348A - Phoenix and Balance
1343B - Balanced Array
1186A - Vus the Cossack and a Contest
1494A - ABC String
1606A - AB Balance
1658C - Shinju and the Lost Permutation
1547C - Pair Programming
550A - Two Substrings
797B - Odd sum
1093A - Dice Rolling
1360B - Honest Coach
1399C - Boats Competition
1609C - Complex Market Analysis
1657E - Star MST
1143B - Nirvana
1285A - Mezo Playing Zoma
919B - Perfect Number
894A - QAQ
1551A - Polycarp and Coins
313A - Ilya and Bank Account
1469A - Regular Bracket Sequence
919C - Seat Arrangements
1634A - Reverse and Concatenate
1619C - Wrong Addition
1437A - Marketing Scheme
1473B - String LCM
1374A - Required Remainder
1265E - Beautiful Mirrors